<!-- MSDS库 -->
<template>
  <el-card class="app-container">
    <div v-if="!open">
      <div class="itemTitle">MSDS库</div>
      <div class="search">
        <el-form :inline="true" :model="queryParams" ref="queryForm">
          <el-col :span="5">
            <el-form-item label="中文名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入中文名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="英文名称" prop="englishName">
              <el-input
                v-model="queryParams.englishName"
                placeholder="请输入英文名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="CAS号" prop="bsCas">
              <el-input
                v-model="queryParams.bsCas"
                placeholder="请输入CAS号"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div class="content">
        <div class="ct-btns">
          <el-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['base:msds:add']"
            >新增</el-button
          >
        </div>
        <div class="ct-table">
          <elTables
            :currentPage="queryParams.pageNum"
            :table-data="tableData"
            :columns="tableColumns"
            :totalLength="total"
            :handle-current-change="handleCurrentChange"
            isPagin
            :curHeight="tableHeight"
          />
        </div>
      </div>
    </div>
    <!-- 新增或修改msds库对话框 -->
    <div v-if="open" class="page-edit">
      <header>
        <i style="font-size: 18px; cursor: pointer; margin-right: 8px" class="el-icon-arrow-left" @click="cancel"></i>
        <span>{{ title }}</span>
      </header>
      <section class="edit-body">
        <div class="form-page">
          <el-form ref="form" :model="form" :rules="rules" label-width="140px">
            <div class="title-level2" style="margin-bottom: 20px">标识</div>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="中文名称" prop="name">
                  <el-input
                    v-model="form.name"
                    placeholder="请输入中文名称"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="英文名称" prop="englishName">
                  <el-input
                    v-model="form.englishName"
                    placeholder="请输入英文名称"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="俗称" prop="bsSc">
                  <el-input
                    v-model="form.bsSc"
                    placeholder="请输入俗称"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="分子式" prop="bsFzs">
                  <el-input
                    v-model="form.bsFzs"
                    placeholder="请输入分子式"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="分子量" prop="bsFzl">
                  <el-input
                    v-model="form.bsFzl"
                    placeholder="请输入分子量"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="CAS号" prop="bsCas">
                  <el-input
                    v-model="form.bsCas"
                    placeholder="请输入CAS号"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="危规号" prop="bsWgh">
                  <el-input
                    v-model="form.bsWgh"
                    placeholder="请输入危规号"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="编号" prop="bsBh">
                  <el-input
                    v-model="form.bsBh"
                    placeholder="请输入编号"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生效日期" prop="effectiveDate">
                  <el-date-picker
                    clearable
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                    v-model="form.effectiveDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择生效日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="title-level2" style="margin-bottom: 20px">理化性质</div>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="性状" prop="lhxzXz">
                  <el-input
                    v-model="form.lhxzXz"
                    placeholder="请输入性状"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="溶解性" prop="lhxzRjx">
                  <el-input
                    v-model="form.lhxzRjx"
                    placeholder="请输入溶解性"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="熔点(℃)" prop="lhxzRd">
                  <el-input
                    v-model="form.lhxzRd"
                    placeholder="请输入熔点(℃)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="沸点(℃)" prop="lhxzFd">
                  <el-input
                    v-model="form.lhxzFd"
                    placeholder="请输入沸点(℃)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="相对密度(水＝1)" prop="lhxzXdmdS">
                  <el-input
                    v-model="form.lhxzXdmdS"
                    placeholder="请输入相对密度(水＝1)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="临界温度(℃)" prop="lhxzLjwd">
                  <el-input
                    v-model="form.lhxzLjwd"
                    placeholder="请输入临界温度(℃)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="临界压力(MPa)" prop="lhxzLjyl">
                  <el-input
                    v-model="form.lhxzLjyl"
                    placeholder="请输入临界压力(MPa)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="相对密度(空气＝1)" prop="lhxzXdmdK">
                  <el-input
                    v-model="form.lhxzXdmdK"
                    placeholder="请输入相对密度(空气＝1)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="燃烧热(KJ/mol)" prop="lhxzRsr">
                  <el-input
                    v-model="form.lhxzRsr"
                    placeholder="请输入燃烧热(KJ/mol)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="最小点火能(mJ)" prop="lhxzZxdhn">
                  <el-input
                    v-model="form.lhxzZxdhn"
                    placeholder="请输入最小点火能(mJ)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="饱和蒸汽压(KPa)" prop="lhxzBhzqy">
                  <el-input
                    v-model="form.lhxzBhzqy"
                    placeholder="请输入饱和蒸汽压(KPa)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div class="title-level2" style="margin-bottom: 20px">
              燃烧爆炸危险性
            </div>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="燃烧性" prop="rsRsx">
                  <el-input
                    v-model="form.rsRsx"
                    placeholder="请输入燃烧性"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="燃烧分解产物" prop="rsRsfjw">
                  <el-input
                    v-model="form.rsRsfjw"
                    placeholder="请输入燃烧分解产物"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="闪点(℃)" prop="rsSd">
                  <el-input
                    v-model="form.rsSd"
                    placeholder="请输入闪点(℃)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="聚合危害" prop="rsJhwh">
                  <el-input
                    v-model="form.rsJhwh"
                    placeholder="请输入聚合危害"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="爆炸极限(V％)" prop="rsBzjx">
                  <el-input
                    v-model="form.rsBzjx"
                    placeholder="请输入爆炸极限(V％)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="爆炸下限(％)" prop="rsBzxx">
                  <el-input
                    v-model="form.rsBzxx"
                    placeholder="请输入爆炸下限(％)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="爆炸上限(％)" prop="rsBzsx">
                  <el-input
                    v-model="form.rsBzsx"
                    placeholder="请输入爆炸上限(％)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="稳定性" prop="rsWdx">
                  <el-input
                    v-model="form.rsWdx"
                    placeholder="请输入稳定性"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="禁忌物" prop="rsJjw">
                  <el-input
                    v-model="form.rsJjw"
                    placeholder="请输入禁忌物"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="引燃温度(℃)" prop="rsYrwd">
                  <el-input
                    v-model="form.rsYrwd"
                    placeholder="请输入引燃温度(℃)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="最大爆炸压力(MPa)" prop="rsZdbzyl">
                  <el-input
                    v-model="form.rsZdbzyl"
                    placeholder="请输入最大爆炸压力(MPa)"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="16">
                <el-form-item label="危险特性" prop="rsWxtx">
                  <el-input
                    v-model="form.rsWxtx"
                    placeholder="请输入危险特性"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="灭火方法" prop="rsMhff">
                  <el-input
                    v-model="form.rsMhff"
                    placeholder="请输入灭火方法"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div class="title-level2" style="margin-bottom: 20px">毒性</div>
            <el-row :gutter="24">
              <el-col :span="16">
                <el-form-item label="毒性" prop="msdsDx">
                  <el-input
                    v-model="form.msdsDx"
                    type="textarea"
                    placeholder="请输入内容"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                    maxlength="300"
                    show-word-limit
                    rows="3"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div class="title-level2" style="margin-bottom: 20px">对人体危害</div>
            <el-row :gutter="24">
              <el-col :span="16">
                <el-form-item label="对人体危害" prop="msdsDrtwh">
                  <el-input
                    v-model="form.msdsDrtwh"
                    type="textarea"
                    placeholder="请输入内容"
                    :readonly="!isEdit"
                    :disabled="!isEdit"
                    maxlength="300"
                    show-word-limit
                    rows="3"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <div class="title-level2" style="margin-bottom: 20px">急救</div>
            <el-col :span="16">
              <el-form-item label="急救" prop="msdsJj">
                <el-input
                  v-model="form.msdsJj"
                  type="textarea"
                  placeholder="请输入内容"
                  :readonly="!isEdit"
                  :disabled="!isEdit"
                  maxlength="300"
                  show-word-limit
                  rows="3"
                />
              </el-form-item>
            </el-col>
          </el-form>
        </div>
      </section>
      <footer>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </footer>
    </div>

    <!-- 导入 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="800px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">取消</el-button>
        <el-button type="primary" @click="submitFileForm">确定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
const defaultQueryParams = {
  pageNum: 1,
  pageSize: 10,
};
import { getToken } from "@/utils/auth";
import {
  pageMsds,
  listMsds,
  getMsds,
  delMsds,
  addMsds,
  updateMsds,
  exportMsds,
} from "@/api/safetyManagement/certificate/msds";

export default {
  name: "Msds",
  data() {
    return {
      tableHeight: 0,
      isEdit: false,
      dialogFormVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/base/msds/importData",
      },
      tableData: [],
      // 查询参数
      queryParams: {
        ...defaultQueryParams,

        name: null,
        englishName: null,
        bsCas: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "中文名称不能为空", trigger: "blur" },
        ],
        englishName: [
          { required: true, message: "英文名称不能为空", trigger: "blur" },
        ],
        bsCas: [{ required: true, message: "CAS号不能为空", trigger: "blur" }],
      },
      tableColumns: [
        { key: "name", title: "中文名称" },
        { key: "bsSc", title: "俗称" },
        { key: "englishName", title: "英文名称" },
        { key: "bsFzs", title: "分子式" },
        { key: "bsFzl", title: "分子量" },
        { key: "bsCas", title: "CAS号" },
        { key: "bsWgh", title: "危规号" },
        {
          title: "操作",
          align: "center",
          fixed: "right",
          width: 200,
          render: (h, params) => {
            return h("div", [
            h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["base:msds:info"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 2);
                    },
                  },
                },
                "详情"
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["base:msds:edit"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleUpdate(params.row, 1);
                    },
                  },
                },
                "编辑"
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "text",
                    size: "small",
                  },
                  directives: [
                    {
                      name: "hasPermi",
                      value: ["base:msds:remove"],
                    },
                  ],
                  on: {
                    click: () => {
                      this.handleDelete(params.row);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
    };
  },
  created() {
    this.getPage();
  },
  mounted() {
    this.$nextTick(() => {
      this.getTableHeight();
    });
  },
  methods: {
    getTableHeight() {
      let height = document.getElementsByClassName("ct-table")[0].clientHeight;
      this.tableHeight = height - 40;
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getPage();
    },
    /** 查询msds库列表 */
    getPage() {
      this.loading = true;
      pageMsds(this.queryParams).then((response) => {
        this.tableData = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        englishName: null,
        bsSc: null,
        bsFzs: null,
        bsFzl: null,
        bsCas: null,
        bsWgh: null,
        bsBh: null,
        effectiveDate: null,
        lhxzXz: null,
        lhxzRjx: null,
        lhxzRd: null,
        lhxzFd: null,
        lhxzXdmdS: null,
        lhxzLjwd: null,
        lhxzLjyl: null,
        lhxzXdmdK: null,
        lhxzRsr: null,
        lhxzZxdhn: null,
        lhxzBhzqy: null,
        rsRsx: null,
        rsRsfjw: null,
        rsSd: null,
        rsJhwh: null,
        rsBzjx: null,
        rsBzxx: null,
        rsBzsx: null,
        rsWdx: null,
        rsJjw: null,
        rsYrwd: null,
        rsZdbzyl: null,
        rsWxtx: null,
        rsMhff: null,
        msdsDx: null,
        msdsDrtwh: null,
        msdsJj: null,
        factoryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createDept: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.isEdit = true;
      this.title = "新增MSDS库";
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      if (type == 1) {
        this.isEdit = true;
      } else {
        this.isEdit = false;
      }
      this.reset();
      const id = row.id || this.ids;
      getMsds(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑MSDS库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMsds(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          } else {
            addMsds(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.$refs.upload.$refs.uploadRef.clearFiles();
              this.open = false;
              this.getPage();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          let data;
          if (Array.isArray(ids)) {
            data = ids;
          } else {
            data = [ids];
          }
          return delMsds(data);
        })
        .then(() => {
          this.getPage();
          this.msgSuccess("删除成功");
        });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getPage();
    },
    // 提交导入文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss" scoped></style>
